package com.efeichong.project.controller;

import com.efeichong.common.PageData;
import com.efeichong.common.domain.ResponseData;
import com.efeichong.project.domain.vo.*;
import com.efeichong.project.service.SysUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author
 * @date 2020/11/25
 * @description 用户
 */
@Api(tags = "用户接口", value = "用户接口")
@RestController
@RequestMapping("/sysUser")
@RequiredArgsConstructor
public class SysUserController {

    private final SysUserService sysUserService;

    @ApiOperation(value = "用户详细")
    @GetMapping(value = "/getInfo")
    public ResponseData<UserInfoVo> getInfo(@RequestParam(value = "id", required = false) Long id) {
        return new ResponseData(sysUserService.getInfo(id));
    }

    @ApiOperationSupport(ignoreParameters = "createBy")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "第几页开始", paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "每页行数", paramType = "query", dataTypeClass = Integer.class)
    })
    @ApiOperation(value = "用户分页查询")
    @GetMapping("/page")
    public ResponseData<PageData<UserListVo>> page(UserQueryVo userQueryVo) {
        return new ResponseData(sysUserService.selectByPage(userQueryVo));
    }

    @ApiOperationSupport(ignoreParameters = {"createBy"})
    @ApiOperation(value = "用户新增")
    @PostMapping("insert")
    public ResponseData insert(@Validated @RequestBody UserInfoVo userInfoVo) {
        sysUserService.insertSysUser(userInfoVo);
        return new ResponseData();
    }

    @ApiOperationSupport(includeParameters = {"id"})
    @ApiOperation(value = "用户编辑")
    @PostMapping("update")
    public ResponseData update(@RequestBody UserInfoVo userInfoVo) {
        sysUserService.updateSysUser(userInfoVo);
        return new ResponseData();
    }

    @ApiOperation(value = "用户批量删除")
    @GetMapping("/deleteByIds")
    public ResponseData deleteSysUserByIds(@RequestParam("ids") List<Long> ids) {
        sysUserService.deleteByIds(ids);
        return new ResponseData();
    }

    @ApiOperation(value = "用户删除")
    @GetMapping("/deleteById")
    public ResponseData deleteSysUserById(@RequestParam("id") Long id) {
        sysUserService.deleteById(id);
        return new ResponseData();
    }

    @ApiOperation(value = "账号密码登陆接口")
    @PostMapping("login")
    public ResponseData login(@RequestBody LoginVo loginVo) {
        return sysUserService.login(loginVo);
    }

    @ApiOperation(value = "获取验证码")
    @GetMapping("getVerifyCode")
    public ResponseData getVerifyCode() {
        return sysUserService.getVerifyCode();
    }

    @ApiOperation(value = "获取用户权限")
    @GetMapping("queryPerms")
    public ResponseData<List<SysMenuVo>> queryPerms() {
        return sysUserService.queryPerms();
    }

    @ApiOperation(value = "更新密码")
    @GetMapping("updatePassword")
    public ResponseData updatePassword(@RequestParam("oldPassword") String oldPassword,
                                       @RequestParam("newPassword") String newPassword) {
        return sysUserService.updatePassword(oldPassword, newPassword);
    }

    @ApiOperation(value = "重置密码")
    @GetMapping("resetPassword")
    public ResponseData resetPassword(@RequestParam("id") Long id) {
        return sysUserService.resetPassword(id);
    }

}
